package com.skywolf.chem.riding.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.riding.entity.Info;
import com.skywolf.chem.riding.pojo.RidingInfoDto;
import com.skywolf.chem.riding.pojo.RidingQueryDto;
import com.skywolf.chem.riding.pojo.RidingQueryParam;
import com.skywolf.chem.riding.service.IInfoService;
import com.skywolf.chem.usercenterapi.entity.CustomerInfo;
import com.skywolf.chem.usercenterapi.service.ICustomerInfoService;
import com.skywolf.chem.utils.IDCardUtil;
import com.skywolf.chem.utils.IdcardValidator;
import com.skywolf.chem.utils.IsWeekSame;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * <p>
 * 骑行表 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-09-07
 */
@Slf4j
@RestController
@RequestMapping("/ct/riding/info")
public class InfoController {

    @Autowired
    IInfoService infoService;

    @Autowired
    ICustomerInfoService customerInfoService;

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");


    @ApiOperation(value = "骑行活动报名",notes = "骑行活动报名")
    @RequestMapping(value = "/signUp",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> signUp(HttpServletRequest request, @RequestBody @Validated RidingInfoDto dto) {
        String uid = request.getHeader("user_id");
        if(dto==null || StringUtils.isBlank(dto.getUserName()) || StringUtils.isBlank(dto.getCardNo())){
            return RespData.failure(ResponseCodeEnum.PARAM_ERROR);
        }
        if(!IdcardValidator.instance().isValidatedAllIdcard(dto.getCardNo())){
            return RespData.failure(ResponseCodeEnum.CARD_ERROR);
        }
        Integer birthdayIng = IDCardUtil.getYear(dto.getCardNo());
        if(birthdayIng<1965 && birthdayIng>2002){
            return RespData.failure(ResponseCodeEnum.AGE_ERROR);
        }
        QueryWrapper<Info> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Info::getUid, Long.valueOf(uid));
        Info entryInfo = infoService.getOne(queryWrapper);
        if(entryInfo!=null){
            return RespData.failure(ResponseCodeEnum.RIDING_ERROR);
        }
        Info entry = new Info();
        BeanUtils.copyProperties(dto,entry);
        entry.setUid(Long.valueOf(uid));
        infoService.save(entry);
        return RespData.success("success");
    }


    @ApiOperation(value = "上报骑行活动数据",notes = "上报骑行活动数据")
    @RequestMapping(value = "/uploadRiding",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> uploadRiding(HttpServletRequest request, @RequestParam(value = "num",required = true) Double num) {
        String uid = request.getHeader("user_id");
        if(num==null){
            return RespData.failure(ResponseCodeEnum.PARAM_ERROR);
        }
        QueryWrapper<Info> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Info::getUid, Long.valueOf(uid));
        Info entryInfo = infoService.getOne(queryWrapper);
        if(entryInfo==null){
            return RespData.failure(ResponseCodeEnum.RIDING_NO_ERROR);
        }
        String nowTime = format.format(new Date());
        String lastTime = dtf2.format(entryInfo.getUpdatedTime());
        Boolean bl = IsWeekSame.isSameDate(nowTime,lastTime);
        infoService.updateRiding(Long.valueOf(uid),num,bl?1:0);
        return RespData.success("success");
    }


    @ApiOperation(value = "查询用户骑行数据",notes = "查询用户骑行数据")
    @RequestMapping(value = "/queryRiding",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<RidingQueryDto> queryRiding(HttpServletRequest request) {
        String uid = request.getHeader("user_id");
        QueryWrapper<Info> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Info::getUid, Long.valueOf(uid));
        Info entryInfo = infoService.getOne(queryWrapper);
        RidingQueryDto dto = new RidingQueryDto();
        if(entryInfo==null){
            dto.setBm(0);
        }else{
            dto.setBm(1);
            BeanUtils.copyProperties(entryInfo,dto);
            if(entryInfo.getNumTotal()==null || entryInfo.getNumTotal()<=0){
                dto.setRanking("暂无排名");
            }else{
                Integer paiming = infoService.getPaiMing(entryInfo.getNumTotal());
                dto.setRanking(paiming.toString());
            }
        }
        CustomerInfo user = customerInfoService.getById(Long.valueOf(uid));
        if(StringUtils.isBlank(dto.getUserName())){
            dto.setUserName(user.getNickName());
        }
        if(StringUtils.isBlank(dto.getUserName())){
            dto.setUserName(user.getBindPhone());
        }
        dto.setHeadImg(user.getHeadImg());
        return RespData.success(dto);
    }


    @ApiOperation(value = "骑行记录榜单数据",notes = "骑行记录榜单数据")
    @RequestMapping(value = "/ridingList",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<RidingQueryDto> ridingList(@RequestBody @Validated RidingQueryParam queryParam) {
        PageVO<RidingQueryDto> page = infoService.pageList(queryParam);
        return RespData.success(page);
    }

}
